#pragma once
#include <mysql/mysql.h>
#include <jsoncpp/json/json.h>
#include <memory>
#include <sstream>
#include <vector>
#include <fstream>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
#include "logger.hpp"


typedef websocketpp::server<websocketpp::config::asio> wsserver_t;

class mysql_util
{
public:
    mysql_util(const char *host, const char *user, const char *password, const char *db, unsigned int port)
    {
        _mysql = mysql_init(nullptr);
        if (_mysql == nullptr)
        {
            LOG(FATAL, "mysql_init ERROR\n");
            exit(-1);
        }

        if (!mysql_real_connect(_mysql, host, user, password, db, port, nullptr, 0))
        {
            LOG(FATAL, "mysql connect error%s\n", mysql_error(_mysql));
            exit(-1);
        }

        // 默认字符集utd-8
        mysql_set_character_set(_mysql, "utf8");
    }

    void set_character_set(const char *set)
    {
        mysql_set_character_set(_mysql, set);
    }

    bool mysql_exec(const std::string &sql)
    {
        int ret = mysql_query(_mysql, sql.c_str());
        if (ret != 0)
        {
            LOG(ERROR, "%s\n", sql.c_str());
            LOG(ERROR, "mysql query failed : %s\n", mysql_error(_mysql));
            return false;
        }
        return true;
    }
    MYSQL *getMysql()
    {
        return _mysql;
    }
    ~mysql_util()
    {
        if (_mysql != nullptr)
            mysql_close(_mysql);
    }

private:
    MYSQL *_mysql;
};

class json_util
{
public:
    static bool serialize(Json::Value &root, std::string &ret)
    {
        Json::StreamWriterBuilder builder;
        std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
        std::stringstream ss;
        writer->write(root, &ss);
        if (!root.empty() && ss.str().empty())
        {
            LOG(ERROR, "serialize error\n");
            return false;
        }
        ret = ss.str();
        return true;
    }
    static bool unserialize(std::string &in, Json::Value &root)
    {
        Json::CharReaderBuilder builder;
        std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
        std::string err;
        if (!reader->parse(in.c_str(), in.c_str() + in.size(), &root, &err))
        {
            LOG(ERROR, "unserialize error%s\n", err.c_str());
            return false;
        }
        return true;
    }
};

class string_util
{
public:
    static int split(const std::string &src, const std::string &sep, std::vector<std::string> &res)
    {
        int prev, cur = 0;
        while (cur < src.size())
        {
            prev = src.find(sep, cur);
            if (prev == std::string::npos)
            {
                res.emplace_back(src.substr(cur));
                break;
            }

            if (cur == prev)
            {
                cur += sep.size();
                continue;
            }
            res.emplace_back(src.substr(cur, prev - cur));
            cur = prev + sep.size();
        }
        return res.size();
    }
};

class file_util
{
public:
    static bool read(const std::string &filePath, std::string &body)
    {
        std::ifstream reader(filePath, std::ios::binary);
        if (!reader.is_open())
        {
            LOG(ERROR, "%s file open fail\n", filePath.c_str());
            return false;
        }

        size_t fileSize = 0;
        reader.seekg(0, std::ios::end);
        fileSize = reader.tellg();
        reader.seekg(0, std::ios::beg);

        body.resize(fileSize);
        reader.read(&body[0], fileSize);
        if (!reader.good())
        {
            LOG(ERROR, "%s file read fail\n", filePath.c_str());
            reader.close(); // 关闭文件！！
            return false;
        }

        reader.close();
        return true;
    }
};